package com.wind.sample.rocketmq.config;

import jakarta.annotation.PostConstruct;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

@Component
public class ConfigValidator {

    private static final Logger logger = LoggerFactory.getLogger(ConfigValidator.class);

    @Autowired(required = false)
    private RocketMQTemplate rocketMQTemplate;

    @Value("${rocketmq.name-server:}")
    private String nameServer;

    @PostConstruct
    public void validate() {
        logger.info("======== RocketMQ 配置校验 ========");
        logger.info("NameServer地址: {}", nameServer);

        if (rocketMQTemplate != null) {
            DefaultMQProducer producer = rocketMQTemplate.getProducer();
            if (producer != null) {
                logger.info("生产者NameServer地址: {}", producer.getNamesrvAddr());
                logger.info("生产者状态: {}", producer.getDefaultMQProducerImpl().getServiceState());
            }
        }
    }
}

